package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Environment
import android.os.Handler
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.repository.AddDriveActivityRepository
import com.cheyun.netsalev3.view.mars.choosecar.MarsChooseCarActivity
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyContentDialogLayout
import com.cheyun.netsalev3.widget.PopDialog
import java.io.Serializable
import android.print.PrintAttributes
import android.print.PrintManager
import com.cheyun.netsalev3.adapter.MyPrintAdapter
import com.cheyun.netsalev3.utils.DownloadUtil
import java.io.File
import android.os.Looper
import android.view.Gravity
import android.widget.DatePicker
import android.widget.ImageView
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.basedata.Expchar
import com.cheyun.netsalev3.bean.basedata.NormalList
import com.cheyun.netsalev3.bean.dms.Product
import com.cheyun.netsalev3.bean.drive.Experience2
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.drive.Reservemit
import com.cheyun.netsalev3.repository.ChooseCarRepository
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiStores
import com.cheyun.netsalev3.view.drivelist.DriveListActivity
import com.cheyun.netsalev3.widget.DatePickerFragment
import com.dou361.dialogui.DialogUIUtils
import com.google.gson.Gson


class ChoiceDriveTimeActivityViewModel(var reserveseries:String,var reservespec:String,var reserveday:String,
                                       var reserveseriesname:String, var reservespecname:String,
                                       var addDriveActivityRepository: AddDriveActivityRepository) : BaseViewModel(){
    var chooseCarRepository = ChooseCarRepository()
    //选择的试驾日期
    var startDay = ObservableField<String>("")
    //选择的试驾时间
    var startTime = ObservableField<String>("")
    //车牌列表
    var infotype = ArrayList<KeyValueParam>()
    var filterType1LeftDate = MutableLiveData<List<KeyValueParam>>()
    var filterType1RgihtList = MutableLiveData<List<NormalList>>()
    var level = "1"
    var licence = ""
    var reservehalf = ""
    init {
        if(reservespec.isNotEmpty()) {
           getdriveseries()
        }
        startDay.set(reserveday)
    }

    fun getdriveseries(){
        level = "1";
        var pid = "";
        if(reserveseries.isNotEmpty()) {
            pid = reserveseries
            level = "2"
        }
        if(reservespec.isNotEmpty()) {
            pid = reservespec
            level = "3"
        }
        chooseCarRepository.getDriveseries(reserveday,level,pid,this::setCarCallback)
    }

    fun setCarCallback(list: List<Product>) {
        if(level.equals("3")) {
            infotype.clear()
            for (licence in list) {
                infotype.add(KeyValueParam(licence.title, 1, licence.id.toString()))
            }
            infotype.forEach { it.IsSelect = false }
            if(infotype.size>0) {
                infotype.get(0).IsSelect = true
                licence = infotype.get(0).key
                addDriveActivityRepository.getTimeList(startDay.get()!!,reserveseries,reservespec,licence,this::setDriveTimes)
            }
            filterType1LeftDate.postValue(infotype.toList())
        }
    }
    fun setDriveTimes(list: List<NormalList>){
//        "is_drive": 0:可预约 1:试驾中 2:已预约 3:已结束
        //status 1可预约 0 已预约 2 试驾中  3 已结束  4 已选择
        for(n in list){
            var ts = n.title.split("~")
            if(ts.size == 2){
                n.title1 = ts[0]
                n.title2 = ts[1]
            }
            if(n.is_drive == 0){
                n.status = R.drawable.sjkexuan
                n.statusName = "可预约"
            }else if(n.is_drive == 1){
                n.status = R.drawable.shijiazhong
                n.statusName = "试驾中"
            }else if(n.is_drive == 2){
                n.status = R.drawable.yijiesuyiyuyue
                n.statusName = "已预约"
            }else if(n.is_drive == 3){
                n.status = R.drawable.yijiesuyiyuyue
                n.statusName = "已结束"
            }else if(n.is_drive == 4){
                n.status = R.drawable.sjyixuanzhong
                n.statusName = "已选择"
            }
        }
        filterType1RgihtList.postValue(list)
    }

    fun submit(view:View){
        var list = filterType1RgihtList.value
        var times = ArrayList<String>()
        for(d in list!!){
            if(d.is_drive == 4){
                times.add(d.title)
            }
        }
        if(times.size == 0){
            MyToast( "请选择预约车牌及时间")
            return
        }
        reservehalf = times.joinToString(",")
        var context = view.context
        if (context is AppCompatActivity) {
            var intent = Intent()
            intent.putExtra("reserveseries",reserveseries)
            intent.putExtra("reservespec",reservespec)
            intent.putExtra("reserveseriesname",reserveseriesname)
            intent.putExtra("reservespecname",reservespecname)
            intent.putExtra("reserveday",reserveday)
            intent.putExtra("licence",licence)
            intent.putExtra("reservehalf",reservehalf)

            context.setResult(103, intent)
            context.finish()
        }
    }

    //选择车牌
    fun onChooseLicence(view:View,item:KeyValueParam){
        infotype.forEach { it.IsSelect = false }
        item.IsSelect = true
        for(l in infotype){
            if(l.key.equals(item.key)){
                l.IsSelect = true
            }
        }
        licence = item.key
        filterType1LeftDate.postValue(infotype)
        addDriveActivityRepository.getTimeList(startDay.get()!!,reserveseries,reservespec,licence,this::setDriveTimes)
    }
    //选择时间
    fun onChooseTime(view:View,item:NormalList){
        var list = filterType1RgihtList.value
        for(d in list!!){
            if(d.title.equals(item.title)){
                if(d.is_drive == 0){
                    d.is_drive = 4
                    d.status = R.drawable.sjyixuanzhong
                    d.statusName = "已选择"
                }else if(d.is_drive == 4){
                    d.is_drive = 0
                    d.status = R.drawable.sjkexuan
                    d.statusName = "可预约"
                }
            }
        }
        filterType1RgihtList.postValue(list)
    }

    //选择日期
    fun onChooseDateType(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetStartDay
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }
    fun onDateSetStartDay(view: DatePicker, year: Int, month: Int, day: Int) {
        startDay.set("$year-$month-$day")
        reserveday = startDay.get()!!
        getdriveseries()
    }

    fun onClicCar(view: View, p: Int) {
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(view.context, MarsChooseCarActivity::class.java)
            intent.putExtra("pos", 1)
//            intent.putExtra("Isseries",true)
            intent.putExtra("Isspec",true)
            intent.putExtra("drive",true)
            intent.putExtra("stime",startDay.get())
            var car = CarBrandParam("", reserveseries, reservespec,"")
            car.seriesname = reserveseriesname
            car.specname = reservespecname
            intent.putExtra("car", car as Serializable)
            content.startActivityForResult(intent, 101)
        }
    }

    /**
     * 设置汽车品牌
     */
    fun setCarData(carBrandParam: CarBrandParam) {
        reserveseries = carBrandParam.series
        reserveseriesname = carBrandParam.seriesname
        reservespec = carBrandParam.spec
        reservespecname = carBrandParam.specname

        getdriveseries()
    }

    override fun onCleared() {
        super.onCleared()
        activity = null
    }

    var activity: AppCompatActivity? = null

}